import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;

/**
 * @author 龙小虬
 * @date 2021/4/14 23:13
 * -Xmx8m
 * -Xmx8m -XX:+PrintGCDetails -verbose:gc
 */
public class SoftReferencesTest {
    public static void main(String[] args) {
        ReferenceQueue<byte[]> referenceQueue = new ReferenceQueue<byte[]>();
        ArrayList<SoftReference<byte[]>> objects = new ArrayList<SoftReference<byte[]>>();
        for (int i = 0; i < 10; i++) {
            SoftReference<byte[]> softReference = new SoftReference<byte[]>(new byte[4 * 1024 * 1024], referenceQueue);
            objects.add(softReference);
        }
        Reference<? extends byte[]> poll = referenceQueue.poll();
        while (poll != null) {
            objects.remove(poll);
            poll = referenceQueue.poll();
        }
        System.out.println("打印结果:");
        objects.forEach((t) -> {
            System.out.println(t.get());
        });
    }
}
